/*
 * Fichier: GenerateArticle.java
 * Crée le: 13 juin 2012.
 * Modifié: 14 juin 2012.
 * Auteurs: Sébastien ESTIENNE.
 * SiteWeb: http://www.prog-info.org/
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package generator.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * <p>Fonctions utilitaires partagées par les générateurs.</p>
 * @author Sébastien ESTIENNE.
 */
public class GeneratorTools
{
   /**
    * Converti un fichier au format Unix.
    * @param filename Le chemin vers le fichier concerné.
    */
   public static void convertFileToUnix(String filename)
   {
      // Lit un fichier et met le contenu dans un buffer.
      File file = new File(filename);
      FileInputStream inStream = null;
      BufferedReader inBuffer = null;
      StringBuilder xmlContent = new StringBuilder();
      try
      {
         inStream = new FileInputStream(file);
         inBuffer = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
         int c;
         while ((c = inBuffer.read()) != -1)
         {
            // Ignore les retours chariot.
            if (c != 13)
            {
               xmlContent.append((char) c);
            }
         }
      }
      catch (IOException ioe)
      {
         ioe.printStackTrace();
         return;
      }
      finally
      {
         if (inBuffer != null)
         {
            try
            {
               inBuffer.close();
            }
            catch (IOException ioe)
            {
               ioe.printStackTrace();
               return;
            }
         }
      }

      // Ecrit le fichier au format Unix.
      FileOutputStream outStream = null;
      BufferedWriter outBuffer = null;
      try
      {
         outStream = new FileOutputStream(file);
         outBuffer = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
         outBuffer.append(xmlContent.toString());
      }
      catch (IOException ioe)
      {
         ioe.printStackTrace();
         return;
      }
      finally
      {
         if (outBuffer != null)
         {
            try
            {
               outBuffer.close();
            }
            catch (IOException ioe)
            {
               ioe.printStackTrace();
            }
         }
      }
   }
}
